<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html  ng-app="App">
<head>
    <title></title>
    <script type="text/javascript" src="bower_components/angular/angular.min.js"></script>
	<script type="text/javascript" src="bower_components/jquery/jquery.min.js"></script>
    <script src='bower_components/jquery-xml2json/src/xml2json.js' type="text/javascript" language="javascript"></script>

    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">


    <script type="text/javascript">

       var myApp = angular.module('App', []);
            myApp.service('dataService', ['$http', function ($http) {


               var server = "http://khesab.aramco.com.sa:1080";
               var applicationPath = "/sap/bc/bsp/sap/zfi_fion_db";
               var hostName = window.location.hostname; //localhost
               var method = "GET";

               var BSP_02 = "/get_variables.xml";
               var BSP_03 = "/getvarf4.xml";
               var BSP_04 = "/execute_report_ad.htm";
               var BSP_05 = "/get_var_info.xml";
               var BSP_06 = "/cost_hier_full.xml";
               var BSP_07 = "/ce_hier_full.xml"


               if(hostName == "localhost" || hostName == "127.0.0.1")
               {
                   BSP_02 = "mock" + BSP_02;
                   BSP_03 = "mock" + BSP_03;
                   BSP_04 = "mock" + BSP_04;
                   BSP_05 = "mock" + BSP_05;
                   BSP_06 = "mock" + BSP_06;
                   BSP_07 = "mock" + BSP_07;
               }

               else
               {
                   BSP_02 = applicationPath + BSP_02;
                   BSP_03 = applicationPath + BSP_03;
                   BSP_04 = applicationPath + BSP_04;
                   BSP_05 = applicationPath + BSP_05;
                   BSP_06 = applicationPath + BSP_06;
                   BSP_07 = applicationPath + BSP_07;
               }

               this.executeQuery = function(queryName, selections)
               {
                   var param = {query: queryName};
                   return $http({method: method, url: BSP_04, params: param});
               }
               this.getF4Help = function (iobjnm, vname, queryName) {

                    var param = {
                        iobjnm: iobjnm,
                        vname: vname,
                        queryName:queryName
                    };

                  return   $http({method: method, url: BSP_03, params: param});
              }

               this.getVarList = function(queryName) {
                    var param = {
                        rep_id: queryName
                    };
                   return  $http({method: method, url: BSP_02, params: param});
                }

               this.getVarInfo = function(varTechName) {

                    var param = {
                        varname: varTechName
                    };

                   return $http({method: method, url: BSP_05, params: param});
                }

               this.getCostCenter = function(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO) {
                    var param = {
                        HIER_NAME: HIER_NAME,
                        IOBJNM: IOBJNM,
                        auth_dso_field: auth_dso_field,
                        AUTH_DSO: AUTH_DSO
                    };

                   return  $http({method: method, url: BSP_06, params: param});
                }

               this.getCostElement = function(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO) {

                    var param = {
                        HIER_NAME: HIER_NAME,
                        IOBJNM: IOBJNM,
                        auth_dso_field: auth_dso_field,
                        AUTH_DSO: AUTH_DSO
                    };

                    $http({method: method, url: BSP_07, params: param});
                }

       }]);

       myApp.controller("FetchCtrl",
        ['$scope', '$templateCache', 'dataService', function($scope, $templateCache, dataService)  {


/* Ok */
            $scope.executeQuery = function(queryName, variables) {
                dataService.executeQuery(queryName, variables)
                        .success(function (data) {
                            $scope.status = status;
                            $scope.data  =  $.xml2json(data);
                          //  $scope.data = data;

							
                        })
                        .error(function (error) {
                            $scope.status = 'Unable to load query data: ' + error.message;
                            $scope.data = data;
                        });
            };


            /* Ok */
            $scope.getF4Help = function (iobjnm, vname, queryName) {
                dataService.getF4Help(iobjnm, vname, queryName).
                        success(function(data, status) {
                            $scope.status = status;
                            $scope.data = $.xml2json(data);
                        }).
                        error(function(data, status) {
                            $scope.data = data || "Request failed";
                            $scope.status = status;
                        });

            };

/* Ok */
            $scope.getVarList = function (queryName) {
                dataService.getVarList(queryName).
                        success(function(data, status) {
                            $scope.status = status;
                            $scope.data = $.xml2json(data);
                        }).
                        error(function(data, status) {
                            $scope.data = data || "Request failed";
                            $scope.status = status;
                        });

            };


            /* Ok */
            $scope.getVarInfo = function (varTechName) {
                dataService.getVarInfo(varTechName).
                        success(function(data, status) {
                            $scope.status = status;
                            $scope.data = $.xml2json(data);
                        }).
                        error(function(data, status) {
                            $scope.data = data || "Request failed";
                            $scope.status = status;
                        });

            };

            /* Ok */
            $scope.getCostCenter = function(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO) {
                dataService.getCostCenter(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO).
                        success(function(data, status) {
                            $scope.status = status;
                            $scope.data = data;
                        }).
                        error(function(data, status) {
                            $scope.data = data || "Request failed";
                            $scope.status = status;
                        });
            };

            /*Ok*/
            $scope.getCostElement = function(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO) {
                dataService.getCostElement(HIER_NAME, IOBJNM, auth_dso_field, AUTH_DSO).
                        success(function(data, status) {
                            $scope.status = status;
                            $scope.data = data;
                        }).
                        error(function(data, status) {
                            $scope.data = data || "Request failed";
                            $scope.status = status;
                        });
            };

        }]);





    </script>
</head>
<body>
<div ng-app="App">

    <div ng-controller="FetchCtrl">
        <button ng-click="executeQuery('ZAD_MAT_01', null)">Execute Query</button><br>
        <button ng-click="getF4Help('0COSTCENTER', '0H_CCTR', 'ZCO_SA_STD_CS_BI7_CEG')">F4 HELP</button>
        <button ng-click="getVarList('ZAD_MAT_01')">GET VARIABLES</button>
        <button ng-click="getVarInfo('X_WHNUMB')">GET VARIABLE INFO</button>
        <button ng-click="getCostCenter('SH_9999', 'COSTCENTER', '' ,'')">GET COST CENTER</button>
        <button ng-click="getCostElement('1_ALL7', '0COSTELMNT', '' ,'')">GET COST ELEMENT</button>

        <pre>http status code: {{status}}</pre>
        <pre>http response data: {{data}}</pre>
    </div>

    <!-- http-hello.html -->
    <script type="text/ng-template" id="http-hello.html">
        Hello, $http!
    </script>
</div>

</body>
</html>